package com.cfets.annualAssessment.service;

import com.cfets.annualAssessment.entity.AnnualInform;
import com.cfets.annualAssessment.entity.Organization;
import com.cfets.annualAssessment.mapper.AnnualInformMapper;
import com.cfets.annualAssessment.mapper.OrganizationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import java.util.List;
/**
 * Created by ShenYiChou on 2017/1/24.
 */
@Service
public class OrganizationService {
    @Autowired
    private OrganizationMapper organizationMapper;
    @Autowired
    private AnnualInformMapper annualInformMapper;
    public List<Organization> getOrganizationPage(Organization organization) {
        Page<?> page = PageHelper.startPage(organization.getPage(), organization.getRows());
        List<Organization> organizations=null;
        Example example=new Example(Organization.class);
        example.setOrderByClause("org_name DESC");
        Example.Criteria criteria =example.createCriteria();
        if(!StringUtils.isEmpty(organization.getOrg_name())){
            criteria.andLike("org_name","%"+organization.getOrg_name()+"%");
        }
        if(!StringUtils.isEmpty(organization.getOrg_code())){
            criteria.andEqualTo("org_code",organization.getOrg_code());
        }
        organizations = organizationMapper.selectByExample(example);
        organization.setTotalCount(page.getTotal());
        return organizations;
    }

    public Organization getOneOrganization(Organization organization) {
        List<Organization> organizations=null;
        Example example=new Example(Organization.class);
        Example.Criteria criteria =example.createCriteria();
        if(!StringUtils.isEmpty(organization.getOrg_code())){
            criteria.andEqualTo("org_code",organization.getOrg_code());
        }
        organizations = organizationMapper.selectByExample(example);
        if(organizations.isEmpty()){
            return organization;
        }
        organization.setId(organizations.get(0).getId());
        organization.setOrg_name(organizations.get(0).getOrg_name());
        organization.setOrg_state(organizations.get(0).getOrg_state());
        return organization;
    }


    public boolean newOrganization(Organization organization) {
        return organizationMapper.insertSelective(organization)>0;
    }

    public boolean updateOrganization(Organization organization) {
        return organizationMapper.updateByPrimaryKey(organization)>0;
    }

    public List<Organization> getAllOrganization(){
        return organizationMapper.selectAll();
    }

    public boolean deleteOrganizationById(int id) {
        Organization organization = organizationMapper.selectByPrimaryKey(id);
        Example example=new Example(Organization.class);
        Example.Criteria criteria =example.createCriteria();
        if(!StringUtils.isEmpty(organization.getOrg_name())){
            criteria.andEqualTo("org_name",organization.getOrg_name());
        }
        List<AnnualInform> annualInforms = annualInformMapper.selectByExample(example);
        if(!annualInforms.isEmpty()){
            return false;
        }
        return organizationMapper.deleteByPrimaryKey(id)>0;
    }
}
